package com.framework.seedkit.utils.ParseUtil;

import android.util.Log;
import org.w3c.dom.Node;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

/**
 * XML报文解析工具库
 */
public class XMLUtil {

	private static final String TAG = XMLUtil.class.getSimpleName();

	private static Transformer getTransformer() {
		Transformer transformer = null;
		try {
			TransformerFactory tf = TransformerFactory.newInstance();
			transformer = tf.newTransformer();
			transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
			transformer.setOutputProperty(OutputKeys.INDENT, "yes");// 设置文档的换行与缩进
			return transformer;
		} catch (TransformerConfigurationException e) {
			Log.e(TAG, "TransformerConfigurationException");
		} catch (IllegalArgumentException e) {
			Log.e(TAG, "IllegalArgumentException");
		} catch (TransformerFactoryConfigurationError e) {
			Log.e(TAG, "TransformerFactoryConfigurationError");
		}
		return null;
	}

	/**
	 * 描述 : 创建xml格式 <br/>
	 * @param node 需要创建的xml Node
	 * @return 返回创建成功的xml字符串
	 */
	public static String getXML(Node node) {
		String xml = null;
		try {
			Transformer transformer = getTransformer();
			if (transformer == null || node == null) {
				return xml;
			}
			DOMSource source = new DOMSource(node);
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			StreamResult result = new StreamResult(bos);
			transformer.transform(source, result);
			xml = bos.toString();
		} catch (TransformerException e) {
			Log.e(TAG, "TransformerException");
		}
		return xml;
	}

	/**
	 * 描述 : xml解析为HashMap
	 * @param xml 需要解析的xml
	 * @return 返回解析后的map
	 */
	public static HashMap<String, String> parse(String xml) {
		HashMap<String, String> map = new HashMap<String, String>();
		try {
			XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
			factory.setNamespaceAware(true);
			XmlPullParser xpp = factory.newPullParser();
			xpp.setInput(new StringReader(xml));
			int eventType = xpp.getEventType();
			String text = null;
			while (eventType != XmlPullParser.END_DOCUMENT) {
				if (eventType == XmlPullParser.START_DOCUMENT) {
				} else if (eventType == XmlPullParser.START_TAG) {
				} else if (eventType == XmlPullParser.END_TAG) {
					map.put(xpp.getName(), text);
				} else if (eventType == XmlPullParser.TEXT) {
					text = xpp.getText();
				}
				eventType = xpp.next();
			}

		} catch (XmlPullParserException e) {
			Log.e(TAG, "XmlPullParserException");
		} catch (IOException e) {
			Log.e(TAG, "IOException");
		}
		return map;

	}

	/**
	 * 解析手机银行参数配置
	 * @param xpp
	 * @return
	 * @throws Exception
     */
	public PhoneBankConfig parsePhoneConfig(XmlPullParser xpp) throws Exception {
		if (xpp == null) {
			return null;
		}
		PhoneBankConfig phoneBankConfig = null;
		List<EnvConfig> envConfigs = null;
		EnvConfig envConfig = null;

		int eventType = xpp.getEventType();
		while (eventType != XmlPullParser.END_DOCUMENT) {
			switch (eventType) {
				case XmlPullParser.START_DOCUMENT:
					phoneBankConfig = new PhoneBankConfig();
					envConfigs = new ArrayList<EnvConfig>();
					break;
				case XmlPullParser.START_TAG:
					if (xpp.getName().equals("envtypeconfig")) {//解析环境配置参数
						eventType = xpp.next();
						phoneBankConfig.setEnvtypeconfig(xpp.getText());
					} else if (xpp.getName().equals("envtype")) {
						envConfig = new EnvConfig();
						String key = xpp.getAttributeName(0);
						Log.d(TAG, "key:" + key + ">>>value:" + xpp.getAttributeValue(0));
						envConfig.setValue(xpp.getAttributeValue(0));
					} else if (xpp.getName().equals("perurl")) {
						eventType = xpp.next();
						envConfig.setPerurl(xpp.getText());
					} else if (xpp.getName().equals("froadshopnet")) {
						eventType = xpp.next();
						envConfig.setFroadshopnet(xpp.getText());
					} else if (xpp.getName().equals("isdebuglog")) {
						eventType = xpp.next();
						envConfig.setIsdebuglog(xpp.getText());
					} else if (xpp.getName().equals("capturescreen")) {
						eventType = xpp.next();
						envConfig.setCapturescreen(xpp.getText());
					} else if (xpp.getName().equals("initlocalresversion")) {
						eventType = xpp.next();
						envConfig.setInitlocalresversion(xpp.getText());
					} else if (xpp.getName().equals("isEncData")) {
						eventType = xpp.next();
						envConfig.setIsEncData(xpp.getText());
					} else if (xpp.getName().equals("checksignbool")) {
						eventType = xpp.next();
						envConfig.setChecksignbool(xpp.getText());
					}
					break;
				case XmlPullParser.END_TAG:
					if (xpp.getName().equals("envtype")) {
						envConfigs.add(envConfig);
						envConfig = null;
					}
					break;
			}
			eventType = xpp.next();
		}
		phoneBankConfig.setEnvconfigs(envConfigs);
		return phoneBankConfig;
	}

	/**
	 * 描述: 具体业务类,为了方便写成内部类
	 */
	public class PhoneBankConfig {

		private String envtypeconfig = "0";
		private List<EnvConfig> envconfigs = null;

		public String getEnvtypeconfig() {
			return envtypeconfig;
		}

		public void setEnvtypeconfig(String envtypeconfig) {
			this.envtypeconfig = envtypeconfig;
		}

		public List<EnvConfig> getEnvconfigs() {
			return envconfigs;
		}

		public void setEnvconfigs(List<EnvConfig> envconfigs) {
			this.envconfigs = envconfigs;
		}
	}
	/**
	 * 具体业务类包含的子类,为了方便写成内部类
	 */
	public class EnvConfig {
		private String value = "0";
		private String perurl = "https://ecom.leagpoint.com/CUPSD/";
		private String froadshopnet = "http://180.163.110.180:26284/o2obill_website/touch/index.jhtml";
		private String isdebuglog = "0";
		private String capturescreen = "0";
		private String initlocalresversion = "14";
		private String isEncData = "0";
		private String checksignbool = "0";

		public String getValue() {
			return value;
		}

		public void setValue(String value) {
			this.value = value;
		}

		public String getPerurl() {
			return perurl;
		}

		public void setPerurl(String perurl) {
			this.perurl = perurl;
		}

		public String getFroadshopnet() {
			return froadshopnet;
		}

		public void setFroadshopnet(String froadshopnet) {
			this.froadshopnet = froadshopnet;
		}

		public String getIsdebuglog() {
			return isdebuglog;
		}

		public void setIsdebuglog(String isdebuglog) {
			this.isdebuglog = isdebuglog;
		}

		public String getCapturescreen() {
			return capturescreen;
		}

		public void setCapturescreen(String capturescreen) {
			this.capturescreen = capturescreen;
		}

		public String getInitlocalresversion() {
			return initlocalresversion;
		}

		public void setInitlocalresversion(String initlocalresversion) {
			this.initlocalresversion = initlocalresversion;
		}

		public String getIsEncData() {
			return isEncData;
		}

		public void setIsEncData(String isEncData) {
			this.isEncData = isEncData;
		}

		public String getChecksignbool() {
			return checksignbool;
		}

		public void setChecksignbool(String checksignbool) {
			this.checksignbool = checksignbool;
		}
	}

}
